package com.idelan.commonMessage.controller;

import com.idelan.commonMessage.constant.ResponseCode;
import com.idelan.commonMessage.constant.RestApi;
import com.idelan.commonMessage.model.RabbitmqReceiveParameter;
import com.idelan.commonMessage.model.RabbitmqSenderParameter;
import com.idelan.commonMessage.utils.RabbitmqReceiverUtil;
import com.idelan.commonMessage.utils.RabbitmqSenderUtil;
import io.swagger.annotations.Api;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
* @Description:    java类作用描述
* @Author:         jzhou
* @CreateDate:     2018\7\19 0019 9:04
* @UpdateUser:     jzhou
* @UpdateDate:     2018\7\19 0019 9:04
* @UpdateRemark:   修改内容
* @Version:        1.0
*/
@RestController
@RequestMapping(value = "/rabbitmq")
@Api("Rabbitmq发送者")
public class RabbitmqController {
    @PostMapping(value="/send")
    public RestApi sendMail(@Valid RabbitmqSenderParameter rabbitmqSenderParametera, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            return RestApi.getErrorApi(ResponseCode.RES_PARAMETER_INVALID, bindingResult.getFieldError().getDefaultMessage());
        }
            return rabbitmqSenderParametera != null ? RestApi.getSuccessApi(
                    RabbitmqSenderUtil.send(
                    rabbitmqSenderParametera.getQueueName(),
                    rabbitmqSenderParametera.getMessage(),
                    rabbitmqSenderParametera.getHost(),
                    rabbitmqSenderParametera.getPort(),
                    rabbitmqSenderParametera.getUserName(),
                    rabbitmqSenderParametera.getPassword())) : null;
    }
    @PostMapping(value="/receive")
    public RestApi sendMail(@Valid RabbitmqReceiveParameter rabbitmqReceiveParameter, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            return RestApi.getErrorApi(ResponseCode.RES_PARAMETER_INVALID, bindingResult.getFieldError().getDefaultMessage());
        }
        return rabbitmqReceiveParameter != null ? RestApi.getSuccessApi(
                RabbitmqReceiverUtil.receive(
                        rabbitmqReceiveParameter.getQueueName(),
                        rabbitmqReceiveParameter.getHost(),
                        rabbitmqReceiveParameter.getPort())) : null;
    }
}
